/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.braincasedesigns.roller.model;

import com.braincasedesigns.roller.parser.model.Modifier;
import com.braincasedesigns.roller.model.Die;
import com.braincasedesigns.roller.model.DieModifier;
import com.braincasedesigns.roller.model.DieRoll;
import com.braincasedesigns.roller.model.DieRollResult;
import com.braincasedesigns.roller.model.Face;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author jcampbel
 */
public class GenericDieRoll implements DieRoll<Die,DieModifier, DieRollResult>{
    private final Die die;
    private final DieModifier modifier;
    private final String notation;
    private final int rolls;
    
    public GenericDieRoll(int rolls, Die die, DieModifier modifier){
        if (rolls < 1){
            throw new IllegalArgumentException("Rolls must be greater than 0:"+rolls);
        }
        this.rolls = rolls;
        this.die = die;
        this.modifier = modifier;
        
        StringBuilder buf = new StringBuilder();
        
        if (rolls > 1){
            buf.append(rolls);
        }
        
        buf.append(die.getNotation());
        if (modifier != null){
            buf.append(modifier.getNotation());
        }
        notation = buf.toString();
    }

    public Die getDie() {
        return die;
    }

    public DieModifier getDieModifier() {
        return modifier;
    }

    public boolean hasModifier() {
        return modifier != null;
    }

    public DieRollResult roll() {
        List<Face> faces = new ArrayList<Face>(rolls);
        for (int i =0; i < rolls; i++){
            faces.add(die.roll());
        }
        return new GenericDieRollResult(this, faces);
    }

    public String getNotation() {
        return notation;
    }

    public int getRolls() {
        return rolls;
    }

}
